import { TaskViewPageType } from "../../../constants/task";
import API from '../../../apis/task';
import { Tools,Toast as message } from 'weaver-mobile';
const { getLabel } = Tools;

export function submit() {
    this.excel.checkInvalidValue();
    if(!this.excel.checkRequire()) {
        return;
    }
    if(!doBefore()) {
        return;
    }

    let datas = {};
    if (this.displayType === TaskViewPageType.EXCEL) {
        if (this.excel) {
            datas = this.excel.dataSourceInstance.getSource();
        }
    } else if (this.displayType === TaskViewPageType.FORM) {
        if (this.form) {
            if (this.form.valid()) {
                datas = this.form.getFields();
            } else {
                //this.components = toJS(this.components);
                return;
            }
        }
    }
    this.loading = true;
    API.submit({
        ...this.params,
        datas: JSON.stringify(datas),
        deletedRows: JSON.stringify(this.excel.deletedRows)
    }).then(this.afterSubmit);

}
export function afterSubmit(result) {
    this.loading = false;
    if (result && result.success) {
        try {
            window.opener._table.reLoad();
        } catch (e) {
        }
        //window.close();
        window.excelHistory.go(-1);
    }
}

export function issue(sheets) {
    this.loading = true;
    API.issue({ ...this.params, sheets }).then(this.afterIssue)
}
export function afterIssue(result) {
    this.loading = false;
    message.success(getLabel('506144','分发成功'));
    if (result && result.success) {
        try {
            window.parent.opener._table.reLoad();
        } catch (e) {
        }
        window.excelHistory.go(-1);
    }
}

export function save() {

    this.excel.checkInvalidValue();
    // if(!this.excel.checkRequire()) {
    //     return;
    // }
    if(!doBefore()) {
        return;
    }

    if(this.loading) {
        return;
    }
    this.loading = true;
    let datas = {};
    let json = "";
    let deletedRows = {};
    if (this.displayType === TaskViewPageType.EXCEL) {
        if (this.excel) {
            datas = this.excel.dataSourceInstance.getSource();
            // json = JSON.stringify(this.excel.workbook.toJSON(false));
            deletedRows = this.excel.deletedRows;
        }
    }
    API.saveData({
        ...this.params,
        'deletedRows': JSON.stringify(deletedRows),
        datas: JSON.stringify(datas),
        json
    }).then(this.afterSave);
}

function doBefore(){
    if(typeof(window.checkCustomize) == "function") {
        return window.checkCustomize();
        
    }
    return true;
}

export function afterSave() {
    this.loading = false;
    this.reloadPage();
    message.success(getLabel('502231', '保存成功'));
}

export function changeLayout(layout) {
    this.layout = layout;
}

export function reloadPage() {
    const workbook = new window.GC.Spread.Sheets.Workbook();
    this.excel.workbook.fromJSON(workbook.toJSON(), false);
    this.excel.workbook.unbindAll();
    this.init(this.params);
}

export function exportExcel4Upload() {
    this.loading = true;
    this.excel.exportExcel4Upload(this.title).then(() => {
        this.loading = false;
    });
}

export function exportExcel() {
    this.loading = true;
    this.excel.exportExcel(this.title);
    this.loading = false;
    
}

export function getShowListDatas(oldFields, newFields) {
    this.excel.getShowListDatas(oldFields, newFields).then(() => {
        // if (newFields.length > 0) {
        //     this.setVisible(false);
        // }
    });
}
export function reject({ id }) {
    this.loading = true;
    API.reject(id).then(this.afterReject)
}

export function afterReject(result) {
    this.loading = false;
    message.success(getLabel('506145','退回成功'));
    if (result && result.success) {
        try {
            window.opener._table.reLoad();
        } catch (e) {
        }
        window.excelHistory.go(-1);
    }
}

export function undo(node) {
    API.undo(node).then(() => {
        this.init(this.params);
        window.location.reload();
    });

}

export function forcedCollect(node) {
    API.forcedCollect(node).then(() => {
        this.init(this.params);
        window.location.reload();
        window.opener._table.reLoad();
        message.success(getLabel('506146','强制收集成功'));
    });
}
export function resetCollect(node) {
    API.resetCollect(node).then(() => {
        this.init(this.params);
        window.location.reload();
        window.opener._table.reLoad();
        message.success(getLabel('506147','覆盖成功'));
    });
}

export function batchReject({ id }) {
    this.loading = true;
    API.batchReject(id).then((result) => {
        this.loading = false;

        message.success(getLabel('506006','批量退回成功'));
        if (result && result.success) {
            try {
                window.opener._table.reLoad();
            } catch (e) {
            }
            window.excelHistory.go(-1);
        }
    });
}